package com.lcxsoft.cartservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lcxsoft.cartservice.entity.Cart;
import com.lcxsoft.cartservice.mapper.CartMapper;
import com.lcxsoft.cartservice.query.CartAddQuery;
import com.lcxsoft.cartservice.service.CartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcxsoft.cartservice.vo.CartVo;
import com.lcxsoft.commonutils.JwtUtils;
import com.lcxsoft.commonutils.R;
import com.lcxsoft.feign.clients.GoodsClient;
import com.lcxsoft.feign.entity.Goods;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcx
 * @since 2022-01-21
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Resource
    private GoodsClient goodsClient;

    @Resource
    private CartMapper cartMapper;

    @Override
    public R addToCartByUserId(CartAddQuery query, HttpServletRequest request) {
        Cart cart = new Cart();
        cart.setCartId(UUID.randomUUID().toString());
        cart.setGoodsAmount(query.getGoodsAmount());
        cart.setGoodsId(query.getGoodsId());
        cart.setUserId(JwtUtils.getMemberIdByJwtToken(request));
        cart.setCreatetime(LocalDateTime.now());
        int insert = cartMapper.insert(cart);
        return insert > 0 ? R.ok().message("添加成功") : R.error().message("添加失败");
    }

    @Override
    public R findCartList(HttpServletRequest request) {
        //1.获取用户ID
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        //2.根据用户ID查到购物车
        List<Cart> cartList = cartMapper.selectList(new LambdaQueryWrapper<Cart>().eq(Cart::getUserId, userId).orderByDesc(Cart::getCreatetime));
        List<CartVo> cartVoList = new ArrayList<>();
        for (int i = 0; i < cartList.size(); i++) {
            Goods goods = goodsClient.queryGoodsById(cartList.get(i).getGoodsId());
            CartVo cartVo = new CartVo();
            cartVo.setGoodsDesc(goods.getGoodsDesc());
            cartVo.setGoodsAmount(cartList.get(i).getGoodsAmount());
            cartVo.setGoodsId(goods.getGoodsId());
            cartVo.setGoodsPic(goods.getGoodsPic());
            cartVo.setCartId(cartList.get(i).getCartId());
            //转换格式保留两位小数
            BigDecimal tottlePrice = goods.getGoodsPrice().multiply(new BigDecimal(cartList.get(i).getGoodsAmount()));
            DecimalFormat df = new DecimalFormat("0.00");
            cartVo.setGoodsTottlePrice(new BigDecimal(df.format(tottlePrice)));

            cartVo.setGoodsName(goods.getGoodsName());
            cartVoList.add(cartVo);
        }
        if (cartVoList.size() == 0){
            return R.error().message("购物是空的");
        }
        return R.ok().data("cartVoList", cartVoList);
    }

    @Override
    public R decreaseCartAmount(String cartId) {
        //根据购物车id获取信息
        Cart cart = cartMapper.selectOne(new LambdaQueryWrapper<Cart>().eq(Cart::getCartId, cartId));
        //如果数量是1直接从表中删除
        if (cart.getGoodsAmount() == 1){
            cartMapper.deleteById(cartId);
        }else {
            cart.setGoodsAmount(cart.getGoodsAmount() - 1);
            cartMapper.updateById(cart);
        }
        return R.ok().message("操作成功");
    }
}
